package auth;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import track.Album;
import track.Artist;
import track.Genre;
import track.Song;
import track.playlistEntry;

import io.*;

/**
 * This is the user class
 * 
 * @author Alex
 *
 */
public class User
{
	/**
	 * Those are all the parameters of the user.
	 * All besides the _id are initialized to null.
	 */
	private int _id;
	private String _username = null;
	private String _email = null;
	private String _name = null;
	private String _password = null;
	private ArrayList<playlistEntry> pEs = new ArrayList<playlistEntry>();
	
	public User(int id)
	{
		this._id = id;
	}
	
	public User(int id, String username, String email, String name, String password)
	{
		//Just setting all the users variables
		this(id);
		this._username = username;
		this._email = email;
		this._name = name;
		this._password = password;
	}
	
	/**
	 * Those are all the get functions for the parameters of the user
	 * 
	 * @return the id of the user
	 */
	public int getId()
	{
		return this._id;
	}
	/**
	 * @return The user name
	 */
	public String getUsername()
	{
		if(_username == null)
			_username = DbFuncs.getContentString("users", "username", "id", this._id);
		
		return _username;
	}
	/**
	 * @return the email of the user
	 */
	public String getEmail()
	{
		if(_email == null)
			_email = DbFuncs.getContentString("users", "email", "id", this._id);
		
		return _email;
	}
	/**
	 * @return the name of the user
	 */
	public String getName()
	{
		if(_name == null)
			_name = DbFuncs.getContentString("users", "name", "id", this._id);
		
		return _name;
	}
	/**
	 * @return the password of the user
	 */
	public String getPassword()
	{
		if(_password == null)
			_password = DbFuncs.getContentString("users", "password", "id", this._id);
		
		return _password;
	}
	
	/**
	 * This function checks if the user is registered in the database
	 * 
	 * @return is registered?
	 */
	public boolean isRegistered()
	{
		//If the id that is returned isn't the user id then he is not registered
		int id = DbFuncs.getContentInt("users", "id", "id", this._id);
		
		return id == this._id;
	}
	
	public boolean isAdmin() {
		// TODO Auto-generated method stub
		return true;
	}
	
	public ArrayList<playlistEntry> getPlaylist(){
		return pEs;
	}
	public boolean addPE(playlistEntry pE){
		if(!isSongInList(pE)){
			pEs.add(pE);
			return true;
		}
		return false;
	}
	public void removePE(playlistEntry pE){
		pEs.remove(pE);
	}
	public void updatePE(playlistEntry pE, Artist artist, Song song, Album album, Genre genre, String Url){
		removePE(pE);
//		pE.updatePE(artist,song,album,genre,Url);
		addPE(pE);
	}
	public boolean isSongInList(playlistEntry pE){
		if(getPlaylist() != null){
			for(playlistEntry p: getPlaylist()){
				if(samePE(p,pE)){
					return true;
				}
			}
		}
		return false;
	}

	private boolean samePE(playlistEntry p, playlistEntry pE) {
		if(p.getAlbum().getId() == pE.getAlbum().getId() && p.getSong().getId() == pE.getSong().getId() && p.getArtist().getId() == pE.getArtist().getId()){
			return true;
		}
		return false;
	}

	public playlistEntry getRandPE() {
		return pEs.get(0);
	}

	public playlistEntry getNonRandPe(int i) {
		return pEs.get(i);
	}

	public void loadList() {
		try{
			FileInputStream filein = new FileInputStream("playlist.ps");
			ObjectInputStream in = new ObjectInputStream(filein);
			pEs = (ArrayList<playlistEntry>)in.readObject();
		}catch(Exception e){}
	}

	public void saveList() {
		try{
			FileOutputStream fileout = new FileOutputStream("playlist.txt");
			ObjectOutputStream out = new ObjectOutputStream(fileout);
			out.writeObject(pEs);
		}catch(Exception e){}
	}
}
